package algorithms.que11;

import com.alipay.api.internal.util.json.StdoutStreamErrorListener;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

/**
 * @功能
 * @创建时间 2020/6/7
 */
public class que35 {
    public static double[] simulation(double[] a,
                                      int N,int SIDES){
        for(int i=0;i<N;i++){
            int n =1+ StdRandom.uniform(SIDES);
            int m = 1+StdRandom.uniform(SIDES);
            a[n+m]=a[n+m]+1.0;
        }
        for (int k=2;k<=2*SIDES;k++){
            a[k]=a[k]/N;
        }
        return a;
    }
    public static void match(double[] dist,double[] dist2,int SIDES,int N){
        boolean IsMAtch=true;
        for(int n =N;IsMAtch;n=n+n/10){
            dist2 = simulation(dist2,n,SIDES);
            int cnt=0;
            for(int k=2;k<=2*SIDES;k++){
                if(Math.abs(dist[k]-dist2[k])>0.0001){
                    IsMAtch=true;continue;
                }else {
                    cnt++;
                }
                if(cnt==11){
                    IsMAtch=false;
                }
            }
            StdOut.println("n="+n+"cnt="+cnt);
        }

    }
    public static void Print(double[] a,int SIDES){
        for(int k=2;k<=2*SIDES;k++){
            StdOut.print("k="+k+a[k]+"");
            if((k-1)%6==0)
                StdOut.print("");
        }
        StdOut.print("");
    }
    public static void main(String[] args) {
        int SIDES=6;
        int N=1000000;
        double[] dist=new double[2*SIDES+1];
        double[] dist2=new double[2*SIDES+1];
        for (int i=1;i<=SIDES;i++)
          for (int j=1;j<=SIDES;j++)
             dist[i+j]+=1.0;
          for (int k=2;k<=2*SIDES;k++){
              dist[k]/=36;
          }
        StdOut.print("dist:");
          Print(dist,SIDES);
          match(dist,dist2,SIDES,N);
    }
}
